package com.gm.wholesale.module.setting.ui.fragment;

import android.util.ArrayMap;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.base.baseBean.BasePageData;
import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseBean.DataResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.base.baseui.baseListviewUI.BaseListFragment;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.network.HttpServicesHolder;
import com.gm.commonlib.views.dialog.loadingDialog.LoadingDialog;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.constant.Constant;
import com.gm.wholesale.constant.PrintType;
import com.gm.wholesale.module.print.api.IPrintApi;
import com.gm.wholesale.module.print.bean.PrintTemplateBean;
import com.gm.wholesale.module.print.model.PrintModel;

import io.reactivex.Observable;

public class OrderPrintTemplateListFragment extends BaseListFragment<PrintTemplateBean, BasePageData<PrintTemplateBean>> {
   public static final String TYPE="type";
    //打印模板类型,1配送单;2.小票

    public int getType(){
          return PrintType.ORDER_PRINT;
    }

    @Override
    protected BaseAdapter createTempBaseAdapter() {

        return new CommonAdapter<>(getActivity(), mDatas,
                R.layout.view_print_temple_item) {
            @Override
            public void convert(ViewHolder viewHolder, final PrintTemplateBean data) {
                viewHolder.setText(R.id.temPNameTv, data.getName());
                ImageView stateImg = viewHolder.getView(R.id.stateImage);
                if (mPrintTemplateBean!=null){
                    if (mPrintTemplateBean.getId()==data.getId()){
                        stateImg.setImageResource(R.drawable.gou_check);
                    }
                    else {
                        stateImg.setImageResource(R.drawable.gou_uncheck);
                    }
                }
                else {
                    stateImg.setImageResource(R.drawable.gou_uncheck);
                }
                viewHolder.getConvertView().setOnClickListener(view -> setPrintTemp(data));

            }

        };
    }

    private void setPrintTemp(PrintTemplateBean bean) {

        LoadingDialog.newLoadingDialog().show(context);
        ArrayMap<String,Object> queryData=new ArrayMap<>();
        queryData.put(TYPE,getType());
        PrintModel.setTemplate(null, bean.getId(), queryData, new BaseHttpObserver<>() {
            @Override
            protected void handleResultOkResp(BaseResp resp) {
                ToastUtil.showToast("操作成功");
                //  bean.setDefaultX(true);
                if (mPrintTemplateBean == null) {
                    mPrintTemplateBean = new PrintTemplateBean();
                }
                mPrintTemplateBean.setId(bean.getId());
                notifyListDataSetChanged();
            }

            @Override
            public void onComplete() {
                LoadingDialog.newLoadingDialog().hid();
            }
        });
    }
   private PrintTemplateBean mPrintTemplateBean;
    @Override
    public void getDataOnCompleted() {
        super.getDataOnCompleted();
        ArrayMap<String,Object> data=new ArrayMap<>();
        data.put(TYPE,getType());
        PrintModel.getTemplateEnable((IBaseView) getActivity(),data, new BaseHttpObserver<>() {
            @Override
            protected void handleResultOkResp(DataResp<PrintTemplateBean> resp) {
                if (resp != null && resp.getData() != null) {
                    mPrintTemplateBean = resp.getData();
                    notifyListDataSetChanged();
                }
            }
        });
    }

    @Override
    protected HttpServicesHolder<?, BasePageData<PrintTemplateBean>> getHttpServicesHolder(long index) {
        return new HttpServicesHolder<IPrintApi, BasePageData<PrintTemplateBean>>() {
            @Override
            public Observable<BasePageData<PrintTemplateBean>> getHttpObservable(IPrintApi api) {
                ArrayMap<String, Object> quearyData = new ArrayMap<>();
                quearyData.put(TYPE,getType());
                quearyData.put("page", index);
                quearyData.put("pageSize", Constant.DEFAULTPAGESIZE);
                return api.getPrintTemplate(quearyData);
            }
        };
    }
}
